package com.szqhyxj.youxiangju.biz.partnerHome.shop.order;

import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.szqhyxj.youxiangju.BasePromptFragment;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.merchantHome.bean.OrderDeetails;
import com.szqhyxj.youxiangju.biz.merchantHome.shop.adapter.OrderAdapter;
import com.szqhyxj.youxiangju.biz.merchantHome.shop.order.OrderContract;
import com.szqhyxj.youxiangju.biz.merchantHome.shop.order.OrderDetailsActivity;
import com.szqhyxj.youxiangju.biz.merchantHome.shop.order.OrderPresenter;
import com.szqhyxj.youxiangju.sys.components.view.popview.MsgChoicePopView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * XiaoPanYu
 * on 2018/11/20
 */
public class MyOrderListFragment extends BasePromptFragment  implements OrderContract.View, OrderAdapter.OnClickitem, MsgChoicePopView.OnClickListener{

    @BindView(R.id.my_orders)
    ListView myOrders;
    @BindView(R.id.no_site_prompt)
    LinearLayout noSitePrompt;

    private List<OrderDeetails> deetails = new ArrayList<>();
    private OrderAdapter adapter;
    private OrderPresenter presenter;
    private MsgChoicePopView choicePopView;
    private int anInt;

    @Override
    protected void start(){
        presenter = new OrderPresenter(this);
        presenter.init();
        adapter = new OrderAdapter(deetails,this);
        myOrders.setAdapter(adapter);
        initEvent();
    }

    @Override
    protected int getLayoutId(){
        return R.layout.fragment_my_order_list;
    }

    private void initEvent(){
        myOrders.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l){
                navOrderDetails(deetails.get(i).getPayOrderId());
            }
        });
    }

    @Override
    public void showOrderList(List<OrderDeetails> deetails){
        myOrders.setVisibility(View.VISIBLE);
        this.deetails.clear();
        this.deetails.addAll(deetails);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void showConfirmOrder(int i){
        deetails.get(i).setReceiverStatus("1");
        adapter.notifyDataSetChanged();
    }

    @Override
    public void showNoOrder(){
        noSitePrompt.setVisibility(View.VISIBLE);
    }

    @Override
    public void navOrderDetails(String orderId){
        OrderDetailsActivity.start(getActivity(), orderId);
    }

    @Override
    public void onBuyNow(int i){
        anInt = i;
        if(choicePopView == null){
            choicePopView = new MsgChoicePopView(getContext(), "请确定是否已收到货?");
            choicePopView.setOnClickListener(this);
            choicePopView.show();
        }
    }

    @Override
    public void onClickConfirm(View v){
        choicePopView.dismiss();
        choicePopView = null;
        presenter.nowConfirm(anInt, deetails.get(anInt).getPayOrderId());
    }
}
